package com.leo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: HelloController2
 * @Description: ${description}
 * @Author: leo825
 * @Date: 2019-12-31 17:01
 * @Version: 1.0
 */

@Controller
@RequestMapping("/test3")
public class HelloController3 {

    /**
     * 返回一个ModeAndView
     *
     * @return
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView hello() {
        System.out.println("使用配置实现 hello controller 跳转到 success");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("data", "恭喜您，测试成功了");
        modelAndView.setViewName("success");//跳转到/WEB-INF/views/success.jsp
        return modelAndView;
    }

    /**
     * 使用Model绑定数据
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/helloModel", method = RequestMethod.GET)
    public String helloModel(Model model) {
        model.addAttribute("data", "使用Model绑定数据");
        return "success";
    }

    /**
     * 使用Model绑定数据
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/helloModelMap", method = RequestMethod.GET)
    public String helloModelMap(ModelMap modelMap) {
        modelMap.addAttribute("data", "使用ModelMap绑定数据");
        return "success";
    }

    /**
     * 返回逻辑视图“success”
     *
     * @return
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public String hello2() {
        System.out.println("访问了 hello2");
        return "success";
    }

    /**
     * 返回一个字符串
     *
     * @return
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/hello3", method = RequestMethod.GET)
    @ResponseBody
    public String hello3() {
        System.out.println("访问了 hello3");
        String hello = "你好 SpringMVC";
        return hello;
    }


    @RequestMapping(value = "/helloUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map helloUserInfo(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "leo");
        map.put("sex", "男");
        return map;
    }
}